home *** CD-ROM | disk | FTP | other *** search
/ Aminet 21 / Aminet 21 (1997)(GTI - Schatztruhe)[!][Oct 1997].iso / Aminet / comm / bbs / cit_src_7H21.lha / route_sys.c < prev    next >
C/C++ Source or Header  |  1997-07-27  |  6KB  |  241 lines

  1. #include "ctdl.h"
  2. #include "2ndfmt.h"
  3. /*
  4.  * Routing records - constructed from ROUTING.SYS.
  5.  */
  6. /**
  7.    External Data
  8. **/
  9.  
  10. extern MessageBuffer   msgBuf;
  11. extern NetTable       *netTab;
  12. extern NetBuffer       netTemp;
  13. extern FILE           *netLog;
  14. extern NetBuffer       netBuf;
  15. extern CONFIG          cfg;
  16. extern AN_UNSIGNED     RecBuf[SECTSIZE + 5];
  17. extern int             thisNet;
  18. extern int             TrError;
  19.  
  20.  
  21. /**
  22.   Type definition created and maintained here
  23. **/
  24. typedef struct
  25.   {
  26.   int  Target, Via;   /* system slots */
  27.   char checked;       /* avoid loops */
  28.   } Routing;
  29.  
  30. extern FILE             *netLog;
  31. extern char              netDebug;
  32.  
  33.  
  34. static int send_direct_mail(int which, char *name);
  35.  
  36.  
  37. /**
  38.      route.sys handling functions
  39.  
  40.   These functions will create a list from the file route.sys.
  41.   The file format is:
  42.  
  43.   target : route
  44.  
  45.   target is a destination for a mail message.  We may have created
  46.   it here on this system or it might have arrived from another system.
  47.  
  48.   route is a node on our nodelist.
  49.  
  50.   OnTarget    -
  51.   EatRoute    -
  52.   RoutingNode - Find the routine node for a given system
  53. */
  54. static void *OnTarget( Routing *element, int *i );
  55. static void *EatRoute( char *line );
  56.  
  57. SListBase Routes = { NULL, OnTarget, NULL, NULL, EatRoute };
  58. /**
  59.   RoutingNode
  60.  
  61.   This function finds the routing node for the given system slot, if any.
  62. **/
  63. int RoutingNode(int slot )
  64.   {
  65.   Routing *record;
  66.  
  67.   if ((record = SearchList(&Routes, &slot)) != NULL)
  68.     {
  69.     if( netDebug )
  70.       {
  71.       splitF(netLog, "RoutingNode(%d): Target Slot: %d  via: %d\n",
  72.       slot, record->Target, record->Via);
  73.  
  74.       };
  75.     return record->Via;
  76.  
  77.     }
  78.     return slot;
  79. }
  80.  
  81. /**
  82.   OnTarget()
  83.  
  84.   This function helps find a routing record based on the Target field.
  85. **/
  86. static void *OnTarget(Routing *element, int *i)
  87. {
  88.     if (element->Target == *i)
  89.       {
  90.       if( netDebug )
  91.         {
  92.         splitF(netLog, "OnTarget(%08.8lx, %d): Target Slot: %d  via: %d\n",
  93.         element, *i, element->Target, element->Via);
  94.         };
  95.  
  96.       return element;
  97.       };
  98.     return NULL;
  99. }
  100.  
  101. /**
  102.   EatRoute()
  103.  
  104.   This function eats a line from ROUTING.SYS.
  105. **/
  106. static void *EatRoute(char *line)
  107. {
  108.     Routing *record;
  109.     char *target, *via;
  110.     int  targind, viaind;
  111.     if( netDebug )
  112.       {
  113.       splitF(netLog, "EatRoute(%s)\n",line);
  114.       };
  115.  
  116.     if ((target = strchr(line, '#')) != NULL) *target = 0;
  117.     if ((target = strtok(line, ":")) == NULL) return NULL;
  118.     if ((via = strtok(NULL, ":")) == NULL)    return NULL;
  119.     NormStr(target);
  120.     NormStr(via);
  121.     if ((targind = searchNameNet(target, &netTemp)) == ERROR) return NULL;
  122.     if ((viaind = searchNameNet(via, &netTemp)) == ERROR) return NULL;
  123.     record = GetDynamic(sizeof *record);
  124.     record->Target = targind;
  125.     record->Via = viaind;
  126.     return record;              /* checked is automatically initialized */
  127. }
  128.  
  129. /*
  130.  * SendOtherRoutedMail()
  131.  *
  132.  * This function sends mail for other systems via a routing node.
  133.  */
  134. void SendOtherRoutedMail(Routing *element, int *net)
  135.   {
  136.   struct cmd_data cmds;
  137.   if( netDebug )
  138.     {
  139.     splitF(netLog, "SendOtherRouteMail(%08.8lx,%d): Target: %d  via: %d\n",
  140.       *net, element->Target, element->Via);
  141.     };
  142.  
  143.   if (element->Via != *net) return;
  144.   if (!netTab[element->Target].ntflags.normal_mail &&
  145.       !netTab[element->Target].ntflags.HasRouted)  return;
  146.  
  147.   if (netTab[element->Target].ntflags.Stadel)  return;
  148.  
  149.   getNet(element->Target, &netTemp);
  150.  
  151.   if (gotCarrier() && netTab[element->Target].ntflags.normal_mail)
  152.     {
  153.     zero_struct(cmds);
  154.     normId(netTemp.netId, cmds.fields[0]);
  155.     strcpy(cmds.fields[1], netTemp.netName);
  156.     cmds.command = ROUTE_MAIL;
  157.     if (sendNetCommand(&cmds, "Route Mail"))
  158.       {
  159.       splitF(netLog, "Routing mail to %s\n", netTemp.netName);
  160.       if (ITL_SendMessages())
  161.         {
  162.         send_direct_mail(element->Target, netTemp.netName);
  163.         if (gotCarrier())
  164.           {
  165.           ITL_StopSendMessages();
  166.           netTemp.nbflags.normal_mail = FALSE;
  167.           }
  168.         }
  169.       else
  170.         {
  171.         splitF(netLog, "ITL_SM failed\n");
  172.         killConnection();
  173.         }
  174.       }
  175.     else
  176.       {
  177.       sprintf(msgBuf.mbtext,"Mail Routing: %s did not recognize %s (%s) for routed mail."
  178.       , netBuf.netName, netTemp.netName, netTemp.netId);
  179.         netResult(msgBuf.mbtext);
  180.       };
  181.     };
  182.   if (gotCarrier() && netTab[element->Target].ntflags.HasRouted)
  183.     {
  184.     RouteOut(&netTemp, element->Target, FALSE);
  185.     }
  186.  
  187.   /* All done - so save what we did */
  188.   putNet(element->Target, &netTemp);
  189. }
  190.  
  191.  
  192. /*
  193.  * send_direct_mail()
  194.  *
  195.  * This sends mail normal (non-route mail).
  196.  */
  197. static int send_direct_mail(int which, char *name)
  198. {
  199.     FILE     *ptrs;
  200.     label    fntemp;
  201.     SYS_FILE fn;
  202.     int      messCount = 0;
  203.     struct   netMLstruct buf;
  204.     extern char *READ_ANY;
  205.  
  206.     sprintf(fntemp, "%d.ml", which);
  207.     makeSysName(fn, fntemp, &cfg.netArea);
  208.     if ((ptrs = fopen(fn, READ_ANY)) == NULL) {
  209.     if (netTab[which].ntflags.normal_mail) {
  210.         sprintf(msgBuf.mbtext, "Send Mail: Mail file for %s missing.",
  211.                             netBuf.netName);
  212.         netResult(msgBuf.mbtext);
  213.     }
  214.     return 0;
  215.     }
  216.  
  217.     while (getMLNet(ptrs, buf) && TrError == TRAN_SUCCESS) {
  218.     if (findMessage(buf.ML_loc, buf.ML_id, TRUE)) {
  219.         if (thisNet != which && netTab[thisNet].ntflags.Stadel) {
  220.         prStStyle(FALSE, getMsgChar, name, sendITLchar, "");
  221.         }
  222.         else
  223.          {
  224.          prNetStyle(FALSE, getMsgChar,  sendITLchar, TRUE, name);
  225.          }
  226.        messCount++;
  227.       }
  228.     }
  229.  
  230.     fclose(ptrs);
  231.     if (TrError == TRAN_SUCCESS) {
  232.     unlink(fn);
  233.     return messCount;
  234.     }
  235.  
  236.     splitF(netLog, "\nFailed transferring mail!\n");
  237.     killConnection();
  238.     return 0;
  239. }
  240.  
  241.